package com.configurations;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;

@Configuration
public class MongoConfig extends AbstractMongoConfiguration {

  @Value("${mongo.db.name}")
  private String mongoDbName;

  @Value("${mongo.host}")
  private String dbHost;

  @Value("${mongo.port}")
  private int dbPort;

  @Value("${mongo.timeout}")
  private int dbTimeout;

  @Bean
  @Override
  public Mongo mongo() throws Exception {
    return new MongoClient(Arrays.asList(new ServerAddress(dbHost, dbPort)), mongoOptions());
  }

  @Bean
  public MongoClientOptions mongoOptions() {
    return MongoClientOptions.builder().connectTimeout(dbTimeout).build();
  }

  @Override
  protected String getDatabaseName() {
    return mongoDbName;
  }
}
